package coalition;

import utils.Util;
import java.util.List;

import population.Population;
import population.PopulationSector;


//wrapper class around List<int[]> to give it clear semantic meaning
//as a coalition, since coalitions are lists of population sectors
//I use an array to specify a sector since we know the number of attributes at
//runtime and it does not change throughout the execution of the program
//I use a list to specify a coalition as a set of sector arrays since we
//calculate many different coalitions dynamically
public class Coalition implements CoalitionInterface{

	Population population;
	
	/* (non-Javadoc)
	 * @see CoalitionInterface#toString()
	 */
	public String toString(){
		return "explicit representation:\n" + toStringLong() + 
		"reduced representation:\n" + toStringReducedRepresentation();
	}

	public String toStringLong(){
		StringBuffer result = new StringBuffer("");
		for(int[] intArray : sectorsInvolved){
			result.append(Util.printIntArray(intArray) + "\n");
		}
		return result.toString();
	}

	public String toStringReducedRepresentation(){
		StringBuffer result = new StringBuffer("");
		for(int[] intArray : sectorsInvolvedReducedRepresentation){
			result.append(Util.printIntArray(intArray) + "\n");
		}
		return result.toString();
	}		

	public Coalition(Population population, List<int[]> sectorsInvolved, List<int[]> sectorsInvolvedReducedRepresentation) {
		super();
		this.sectorsInvolvedReducedRepresentation = sectorsInvolvedReducedRepresentation;
		this.sectorsInvolved = sectorsInvolved;
		this.population = population;
	}

	/* (non-Javadoc)
	 * @see CoalitionInterface#getSectorsInvolved()
	 */
	public List<int[]> getSectorsInvolved() {
		return sectorsInvolved;
	}

	List<int[]> sectorsInvolved;
	List<int[]> sectorsInvolvedReducedRepresentation;

	/* (non-Javadoc)
	 * @see CoalitionInterface#getCoalitionPopulationSize()
	 */
	public int getCoalitionPopulationSize(){
		int populationSize = 0;
		for(int[] sectorIndices : sectorsInvolved){
			PopulationSector populationSector = population.getSector(sectorIndices);
			populationSize += populationSector.getSize();
		}
		return populationSize;
	}

	/* (non-Javadoc)
	 * @see CoalitionInterface#getModulatedCoalitionPopulationSize()
	 */
	public float getModulatedCoalitionPopulationSize(){
		float populationSize = 0;
		for(int[] sectorIndices : sectorsInvolved){
			PopulationSector populationSector = population.getSector(sectorIndices);
			populationSize += populationSector.getSize()*populationSector.getPower();
		}
		return populationSize;
	}

	@Override
	public List<int[]> getReducedRepresentationOfSectorsInvolved() {
		return sectorsInvolvedReducedRepresentation;
	}		

}